const { logInfo, logError } = require("../logger/index");
const jwt = require("jsonwebtoken");
const { User } = require("../models");
const { BadRequest, Unauthorized, NotFound } = require("http-errors");
const loggerMiddleware = async (req, res, next) => {
  const start = Date.now();

  let user = "";
  try {
    logInfo("请求开始", {
      method: req.method,
      route: req.originalUrl,
    });
    await next();

    const ms = Date.now() - start;
    logInfo("请求完成", {
      method: req.method,
      route: req.originalUrl,
    });
  } catch (err) {
    const ms = Date.now() - start;
    logError("请求异常", err, {
      method: req.method,
      route: req.originalUrl,
    });
    throw err; // 继续抛出错误，让错误处理中间件处理
  }
};

module.exports = loggerMiddleware;
